﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Results_Comparer.Properties;

namespace Results_Comparer
{
    public class HierarchicalViewFormatter
    {
        // Fields
        private TreeNode root;

        // Methods
        public HierarchicalViewFormatter(TreeNode root)
        {
            this.root = root;
        }

        private void AddRdfId(string rdfId, string name, StreamWriter f)
        {
            if (name.Contains(rdfId))
            {
                f.Write("\n<li><A href=\"contents.html#" + rdfId + "\" target=\"contents\" >" + name + "</A></li>");
            }
            else
            {
                f.Write("\n<li><A href=\"contents.html#" + rdfId + "\" target=\"contents\" >" + name + " (" + rdfId + ")</A></li>");
            }
        }

        private void AddTableHeader(StreamWriter f)
        {
            f.Write("\n<TABLE WIDTH=\"100%\" BORDER=\"0\"><TR><TD WIDTH=\"100%\" ALIGN=\"LEFT\" VALIGN=\"TOP\">");
            f.Write("\n<A href=\"#\" onClick=\"expandDifferences('tree1'); return false;\">Expand All</A>&nbsp;&nbsp;&nbsp;");
            f.Write("\n<A href=\"#\" onClick=\"collapseDifferences('tree1'); return false;\">Collapse All</A>&nbsp;&nbsp;&nbsp;");
            f.Write("\n<ul class=\"mktree\" id=\"tree1\">");
        }

        private void CreateDisplay(TreeNode root, StreamWriter f)
        {
            if (root != null)
            {
                List<TreeNode> childs = root.childs;
                foreach (TreeNode node in childs)
                {
                    this.CreateDisplays(node, f);
                }
            }
        }

        private void CreateDisplays(TreeNode root, StreamWriter f)
        {
            if (root != null)
            {
                if (root.myRdfId != null)
                {
                    this.AddRdfId(root.myRdfId, root.name, f);
                }
                else
                {
                    f.Write("<li>" + root.name + "\n");
                }
                f.Write("<ul>\n");
                List<TreeNode> childs = root.childs;
                foreach (TreeNode node in childs)
                {
                    this.CreateDisplays(node, f);
                }
                f.Write("\n</ul>");
                f.Write("</li>");
            }
        }

        public void format(StreamWriter sW)
        {
            sW.Write("<html>\n");
            sW.Write("<head>\n");
            sW.Write("<h4 align=\"center\">Hierarchical View</h4>\n");
            sW.Write("<SCRIPT LANGUAGE=\"JavaScript\">");
            sW.Write(Resources.diffs);
            sW.Write("</SCRIPT>");            
            sW.Write(Resources.css);            
            sW.Write("</head>\n");
            sW.Write("<body  BGCOLOR=#FFFFFF LINK=\"#00615F\" VLINK=\"#00615F\" ALINK=\"#00615F>\n");
            this.AddTableHeader(sW);
            this.CreateDisplay(this.root, sW);
            sW.Write(" </TD><TD VALIGN=\"TOP\"></TD></TR></TABLE> \n");
            sW.Write("</body>\n");
            sW.Write("</html>\n");
        }
    }
}